import api from "@/api";
import {asyncCompute, dict} from "@fast-crud/fast-crud";

export default function ({expose}) {
    const pageRequest = async (query) => {
        return api.userController.page({
            pageParam: query.page,
            specification: query.form
        });
    };
    const editRequest = async ({form, row}) => {
        form.id = row.id;
        return await api.userController.update({
          body: form
        });
    };
    const delRequest = async ({row}) => {
        return await api.userController.deleteByIds({
            body: [row.id]
        });
    };

    const addRequest = async ({form}) => {
        return await api.userController.add({
            body: form
        });
    };
    return {
        crudOptions: {
            request: {
                pageRequest,
                addRequest,
                editRequest,
                delRequest
            },
            search: {
                autoSearch: false
            },
            rowHandle: {
                fixed: "right"
            },
            table: {
                scroll: {
                    //使用固定列时需要设置此值，并且大于等于列宽度之和的值
                    x: 1400
                }
            },
            columns: {
                id: {
                    title: "ID",
                    form: {show: false}, // 表单配置
                    column: {
                        width: 70,
                    }
                },
                username: {
                    title: "用户名",
                    type: "text",
                    search: {show: true}, // 开启查询
                    form: {
                        rules: [
                            {required: true, message: "请输入用户名"},
                            {max: 50, message: "最大50个字符"}
                        ]
                    },
                    editForm: {component: {disabled: true}},
                    column: {
                        sorter: true
                    }
                },
                password: {
                    title: "密码",
                    type: "text",
                    key: "password",
                    column: {
                        show: false
                    },
                    form: {
                        rules: [{max: 50, message: "最大50个字符"}],
                        component: {
                            showPassword: true
                        },
                        helper: "填写则修改密码"
                    }
                },
                nickname: {
                    title: "昵称",
                    type: "text",
                    search: {show: true}, // 开启查询
                    form: {
                        rules: [{max: 50, message: "最大50个字符"}]
                    },
                    column: {
                        sorter: true
                    }
                },
                /* avatar: {
                  title: "头像",
                  type: "cropper-uploader",
                  column: {
                    width: 100,
                    component: {
                      //设置高度，修复操作列错位的问题
                      style: {
                        height: "30px",
                        width: "auto"
                      }
                    }
                  }
                }, */
                roleIds: {
                    title: "角色",
                    type: "dict-select",
                    dict: dict({
                        getData: ({row}) => row && api.roleController.list({
                            roleSpecification: {
                                userId: row.id
                            }
                        }),
                        value: 'id',
                        label: 'name',
                    }), // 数据字典
                    form: {
                        component: {multiple: true}
                    },
                    column: {
                        width: 250
                    }
                },
                createTime: {
                    title: "创建时间",
                    type: "datetime",
                    form: {show: false}, // 表单配置
                    column: {
                        width: 180,
                        sorter: true
                    }
                }
            }

        }
    };
}
